//
//  RangeSliderWraper.m
//  HomeLink
//
//  Created by GaoYP on 2012年.
//  Copyright (c) 2012 HomeLink. All rights reserved.
//

#import "RangeSliderWraper.h"

#import "RangeSlider.h"

@implementation RangeSliderWraper

@synthesize minimumValue, maximumValue, minimumRange, selectedMinimumValue, selectedMaximumValue;
@synthesize title = _title;
@synthesize unitName = _unitName;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        lblRangeValue =  [[UILabel alloc] init];
        lblRangeValue.frame = CGRectMake(112, 5, 140, 15);
        lblRangeValue.textAlignment = UITextAlignmentCenter;
        lblRangeValue.textColor = RGBCOLOR(0x0, 0x47, 0x14);
        lblRangeValue.backgroundColor = [UIColor clearColor];
        lblRangeValue.font = [UIFont boldSystemFontOfSize:12];
        
        _lblTitle = [[UILabel alloc] init];
        _lblTitle.frame = CGRectMake(10,23,80,40);
        _lblTitle.backgroundColor = [UIColor clearColor];
        _lblTitle.font = [UIFont systemFontOfSize:15.0f];
        
        _slider =  [[RangeSlider alloc] initWithFrame:CGRectMake(73, 18, 228, 40)];
        _slider.tag = 100 + 1;
        _slider.frame = CGRectMake(73, 23, 228, 40);
        _slider.minimumValue = 0;
        [_slider addTarget:self action:@selector(updateRangeLabel) forControlEvents:UIControlEventValueChanged];
        
        
        lblMinValue = [[UILabel alloc] init];
        lblMinValue.frame = CGRectMake(85, 62, 30, 15);
        lblMinValue.text = @"0";
        lblMinValue.backgroundColor = [UIColor clearColor];
        lblMinValue.textColor = RGBCOLOR(0x22, 0x22, 0x22);
        lblMinValue.font = [UIFont systemFontOfSize:12];
        
        lblMaxValue = [[UILabel alloc] init];
        lblMaxValue.frame = CGRectMake(185, 62, 100, 15);
        lblMaxValue.backgroundColor = [UIColor clearColor];
        lblMaxValue.textColor = RGBCOLOR(0x22, 0x22, 0x22);
        lblMaxValue.textAlignment =  UITextAlignmentRight;
        lblMaxValue.font = [UIFont systemFontOfSize:12];

              
        [self addSubview:lblRangeValue];
        [self addSubview:_lblTitle];
        [self addSubview:_slider];
        [self addSubview:lblMinValue];
        [self addSubview:lblMaxValue];
        

    }
    return self;
}

//view将要显示时调用此方法
-(void)layoutSubviews
{
    _slider.minimumRange = minimumRange;
    _slider.minimumValue = minimumValue;
    _slider.maximumValue = maximumValue;  
    _slider.selectedMinimumValue = selectedMinimumValue;
    _slider.selectedMaximumValue = selectedMaximumValue;
    
    _lblTitle.text = _title;
    lblMinValue.text = [NSString stringWithFormat:@"%.0f",minimumValue];
    lblMaxValue.text = [NSString stringWithFormat:@"%.0f%@以上",maximumValue,_unitName];
    
    [self updateRangeLabel];

}

-(void)updateRangeLabel{
    lblRangeValue.text = [self getRangeLiteryWithIndex:0 min:round(_slider.selectedMinimumValue) max:round(_slider.selectedMaximumValue)];
}


- (NSString*)getRangeLiteryWithIndex:(int)index min:(int)min max:(int)max
{
    NSString* rangeLitery = nil;
    
    if(min == 0 && max == maximumValue)
    {
        rangeLitery = @"不限";
    }else if(min == 0)
    {
        rangeLitery = [NSString stringWithFormat: @"%d%@以下",max,_unitName];
    }else if(max == maximumValue)
    {
        rangeLitery = [NSString stringWithFormat: @"%d%@以上",min,_unitName];
    }
    else
    {
        if(max < min)
        {
            int temp = max;
            max = min;
            min = temp;
        }
        
        rangeLitery = [NSString stringWithFormat: @"%d%@-%d%@",min,_unitName,max,_unitName];
    }    
    
    return rangeLitery;
}

-(float)getSelectedMinimumValue
{
    return _slider.selectedMinimumValue;
}

-(float)getSelectedMaximumValue
{
    return _slider.selectedMaximumValue;
}
    
@end
